this.name           = "hyperradio";
this.author         = "Svengali";
this.copyright      = "CC-by-nc-sa-3.0";
this.description    = "Expandable by musicpacks. Compatible to OXPConfig and RepairBots.";
this.version        = "1.16";

/* ***** CHANGE ONLY THIS PART - Add new or override the following entries to use it as playList ***** */
this.hyperradio_playListOriginal = new Array(
                        "hyperradio-exampleA.ogg",
                        "Erik_Ostrom-By_Your_Side.ogg",
                        "Josh_Woodward-On_Brevity.ogg",
                        "Josh_Woodward-Untitled.ogg",
                        "Paul_Westerberg-Looking_up_In_Heaven.ogg",
                        "William_Brooks-Seven_Promises.ogg",
                        "Aching_Beauty-Steps.ogg",
                        "Antarhes-Different_Way.ogg",
                        "Aygan-Now_Its_Over.ogg",
                        "Holy_Pain-Last_Sigh.ogg",
                        "Red_Light_Rippers-Addicted.ogg",
                        "Droopy_Contini-Dewey_Square.ogg",
                        "La_Rabona-Vamos_a_Reconquistar.ogg",
                        "Lasswell-Drunk_In_A_Smokey_Bar_Drinking_An_Imperial_Porter_Mix.ogg",
                        "Luc_Bartoli-Hassans_Dream.ogg",
                        "Matt_Pepper_Trio-Jai_demande_au_soleil.ogg",
                        "SuperNova-Love.ogg",
                        "mindthings-Emotion_Vibes.ogg",
                        "paniq-occidental_nemesis.ogg",
                        "SoLaRiS-Most_Haunted.ogg",
                        "Vate-Diablo_(en vivo).ogg",
                        "Our Lifes, Our Destinies.ogg",
                        "Sounds From The Past.ogg",
                        "Emotion Vibes.ogg",
                        "Night Rains.ogg",
                        "Life's Things.ogg",
                        "Artificial World.ogg",
                        "Cold Water.ogg",
                        "Exponential Tears.ogg",
                        "Reversal Situation.ogg",
                        "The Last Time On This Earth.ogg",
                        "AfterLife.ogg",
                        "Rebirth.ogg",
                        "hyperradio-exampleB.ogg"
);
/* *************************************************************************************************** */
this.stationList = new Array("hyperradio_playListOriginal");

this.startUp = function()
{
/* OXPConfig audio = collect entries from packs into main playlist, extraA = EscapePod Music, extraB = Sort lists randomly */
        this.logging = false; this.audio = true; this.extraA = true; this.extraB = false;
}
this.reset = function()
{
        this.stations = this.stationList.length; this.limit = 0; var counterLimit = this.stations; this.current = 'start'; this.next = 'start';
        while(counterLimit >= 1) { counterLimit *= 0.5; this.limit++; }
        this.limit = Math.pow(2,this.limit)-1;
        if(this.logging) log('Hyperradio','Hyperradio: Stations found:'+this.stations+' Limit:'+this.limit);
        this.hyperRadio = false; this.canPlay = false; this.addDuration = 0;
        if(this.audio && this.stations > 1) { var outerLoop = 1; var innerLoop = 0; var lname = ""; var sname = "";
                        for(outerLoop = 1; outerLoop < this.stationList.length; outerLoop++) { sname = worldScripts.hyperradio[this.stationList[outerLoop]];
                                for(innerLoop = 0; innerLoop < sname.length; innerLoop++) { lname = this.stationList[outerLoop];
                                        this.hyperradio_playListOriginal.push(worldScripts.hyperradio[lname][innerLoop]); }}
                        var cutIDA = this.hyperradio_playListOriginal.indexOf('hyperradio-exampleA.ogg');
                        if(cutIDA != -1){ if(!cutIDA) this.hyperradio_playListOriginal.shift();
                                else { var a = this.hyperradio_playListOriginal.slice(0,cutIDA); var b = this.hyperradio_playListOriginal.slice(cutIDA+1,this.hyperradio_playListOriginal.length);
                                        var c = a.concat(b); this.hyperradio_playListOriginal = c; }}
                        var cutIDB = this.hyperradio_playListOriginal.indexOf('hyperradio-exampleB.ogg');
                        if(cutIDB != -1){ if(!cutIDB) this.hyperradio_playListOriginal.shift();
                                else { var d = this.hyperradio_playListOriginal.slice(0,cutIDB); var e = this.hyperradio_playListOriginal.slice(cutIDB+1,this.hyperradio_playListOriginal.length);
                                        var f = d.concat(e); this.hyperradio_playListOriginal = f; }}
                        if(this.logging) log('Hyperradio','Hyperradio Main Playlist contains now '+this.hyperradio_playListOriginal.length+' songs. Examples files removed from list.'); }
}
this.playerBoughtEquipment = function(equipmentKey)
{
        if(equipmentKey == "EQ_HYPER_RADIO") { this.hyperRadio = true; EquipmentInfo.infoForKey("EQ_HYPER_RADIO_ADD").effectiveTechLevel = 3; }
        if(equipmentKey == "EQ_HYPER_RADIO_ADD") { this.hyperRadio = true; this.addDuration = 100; player.ship.removeEquipment("EQ_HYPER_RADIO_ADD");
                missionVariables.TL_FOR_EQ_HYPER_RADIO_ADD = null; }
}
this.equipmentDamaged = this.equipmentDestroyed = function(equipmentKey)
{
        if(equipmentKey == "EQ_HYPER_RADIO") { this.canPlay = false; if(this.current != 'start') Sound.stopMusic(this.current); }
}
this.shipWillDockWithStation = function()
{
        if(this.current != 'start') Sound.stopMusic(this.current);
        this.canPlay = false; this.CheckHyperradioLicence();
}
this.shipWillLaunchFromStation = function()
{
        if(!this.current) this.reset();
        missionVariables.TL_FOR_EQ_HYPER_RADIO_ADD = null;
        if(player.ship.hasEquipment("EQ_HYPER_RADIO")) {
                if(system.isInterstellarSpace) { this.canPlay = false; return; }
                this.hyperRadio = true;
                if(this.extraB) { this.stationList.sort(function() { return 0.5-Math.random(); } ); this.extraB = false; }
                if(this.stations > 1) this.SelectHyperradioStation();
                else this.stationSelect = 0; }
}
this.shipLaunchedFromStation = function()
{
        if(this.hyperRadio && !system.isInterstellarSpace) { this.current = 'start'; this.next = 'start';
                if(!missionVariables.hyperradio_last) missionVariables.hyperradio_last = String(Math.floor(100 +(clock.days)));
                if(this.addDuration) missionVariables.hyperradio_last += this.addDuration;
                this.canPlay = true; this.addDuration = 0; }
}
this.shipExitedWitchspace = function()
{
        if(this.hyperRadio) {
                if(system.isInterstellarSpace) { if(this.current != 'start') Sound.stopMusic(this.current);
                        player.commsMessage('Hyperradio deactivated. Could not transfer data.', 8); this.canPlay = false; return; }
                else if(this.stations > 1) this.SelectHyperradioStation();
                        else this.stationSelect = 0;
                        this.canPlay = true; }
}
this.alertConditionChanged = function()
{
        if(this.hyperRadio) { if(player.alertCondition == 3) { if(this.current != 'start') Sound.stopMusic(this.current); }
                else if(player.alertCondition < 3 && !player.ship.docked && !system.isInterstellarSpace) this.canPlay = true; }
}
this.shipLaunchedEscapePod = function()
{
        if(this.extraA) { if(this.current != 'start') Sound.stopMusic(this.current); Sound.playMusic('hyperradio-escapepod.ogg'); }
}
this.SelectHyperradioStation = function()
{
        this.stationSelect = system.ID&this.limit;
                if(this.logging) log('Hyperradio','Hyperradio: SELECTEDA:'+this.stationSelect+' in System:'+system.ID);
        if(this.stationSelect == 0 && Math.random() > 0.5) { this.stationSelect += Math.floor(Math.random()*(this.stations-1))+1;
                if(this.stationSelect >(this.stations-1)) { this.stationSelect = this.stations-1; }
                if(this.logging) { log('Hyperradio','Hyperradio: SELECTEDB:'+this.stationSelect); }}
        if(this.stationSelect >(this.stations-1)) { this.stationSelect -= Math.ceil(Math.random()*(this.stations-1))+1;
                this.stationSelect = Math.min((this.stations-1),this.stationSelect); if(this.logging) log('Hyperradio','Hyperradio: SELECTEDC:'+this.stationSelect); }
        return;
}
this.GetHyperradioSongName = function()
{
        var listName = this.stationList[this.stationSelect]; var songNames = worldScripts.hyperradio[listName]; var maxSongs = worldScripts.hyperradio[listName].length-1;
        var songSelect = Math.floor(Math.random()*maxSongs); var songName = songNames[songSelect];
        if(!listName || !songName || !songName.match('.ogg') || maxSongs == -1) { player.consoleMessage('Hyperradio: NO VALID ENTRY FOUND! See Logfile for Details.',8);
                log('Hyperradio','Hyperradio: WARNING. NO VALID ENTRY IN PLAYLIST FOUND. Error in:'+listName+' '+songName+' Station:'+this.stationSelect); return(false); }
        this.next = songName; while(this.current == this.next) { songSelect = Math.floor(Math.random()*maxSongs); this.next = songNames[songSelect]; }
        if(this.logging) log('Hyperradio','Hyperradio: Station:'+listName+' Song:#'+songSelect+' out of #'+maxSongs+' in this list.');
        return(true);
}
this.CheckHyperradioLicence = function()
{
        if(clock.days >= parseInt(missionVariables.hyperradio_last)) {
                player.ship.removeEquipment("EQ_HYPER_RADIO");
                this.hyperRadio = false; missionVariables.hyperradio_last = null; this.canPlay = false;
                player.consoleMessage('Your HyperRadio license is expired. Buy a new one at your local dealer.', 6);
                return(false); }
        if(system.isInterstellarSpace) return(false);
        if(player.ship.equipmentStatus("EQ_HYPER_RADIO") == "EQUIPMENT_DAMAGED") { this.canPlay = false;
                player.consoleMessage('Can not connect to next HyperRadio station. Device damaged.', 6); return(false); }
        if(player.ship.equipmentStatus("EQ_HYPER_RADIO") == "EQUIPMENT_OK") this.canPlay = true;
        return(true);
}
this.guiScreenChanged = function(to)
{
        if(player.ship.status == "STATUS_IN_FLIGHT" && to == "GUI_SCREEN_SYSTEM_DATA") {
                if(this.hyperRadio && this.CheckHyperradioLicence() && this.canPlay) { if(this.current != 'start') Sound.stopMusic(this.current);
                        if(!this.GetHyperradioSongName() || this.next == this.stationList[this.stationSelect]) { this.canPlay = false; return; }
                        else { this.current = this.next; player.consoleMessage('Connection established. Days left: '+(missionVariables.hyperradio_last-clock.days)); Sound.playMusic(this.current); }}}
}